import axios from 'axios'
import { ElMessage } from 'element-plus'

const service = axios.create({
  baseURL: 'http://localhost:3000',
  timeout: 10000
})

// 请求拦截器
service.interceptors.request.use(
  (config) => {
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    if (response.data.data.success) {
      return response.data
    } else {
      ElMessage.error(response.data.msg)
      return Promise.reject(response)
    }
  },
  (error) => {
    if (error.response.status === 401) {
      ElMessage.error(error.response.data.msg)
      return Promise.reject(error)
    }
    return Promise.reject(error)
  }
)
export default service
